import { getSupplierMaterialList, putOnOrOff, postDeleteMaterial } from '../../../api/index'

Page({
  data: {
    list: [],
    pageSize: 15,
    currentPage: 1,
    keyword: '',
    reviewStatus: 0,
    reviewStatusStr: '综合',
    showFilter: false,
    filterColumns: [
      { label: '综合', value: 0 },
      { label: '已上架', value: 1 },
      { label: '已下架', value: 2 }
    ]
  },
  seeDetail ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/pages/materialDetail/index?id=${item.id}`)
  },
  async deleteMaterial ({ currentTarget: { dataset: { item, index } } }) {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '删除材料', content: '是否删除该材料？' })
      if (confirm) {
        wx.showLoading({ mask: true })
        await postDeleteMaterial({ materialId: item.id })
        let swiper = this.selectComponent(`#swiper-cell-${item.id}`)
        swiper.close()
        wx.hideLoading()
        wx.showToast({ title: '删除成功！', icon: 'none' })
        this.data.list.splice(index, 1)
        this.setData({ list: this.data.list })
      }
    } catch (err) {
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async shelves ({ currentTarget: { dataset: { item, index } } }) {
    try {
      let modalTitle = item.isOnSales ? '下架材料' : '上架材料'
      let contentTitle = item.isOnSales ? '是否下架该材料？' : '是否上架该材料？'
      const { confirm } = await wx.$pro.showModal({ title: modalTitle, content: contentTitle })
      if (confirm) {
        let query = {
          materialId: item.id,
          isOnSales: !item.isOnSales
        }
        wx.showLoading({ mask: true })
        await putOnOrOff({ ...query })
        wx.hideLoading()
        wx.showToast({ title: '操作成功！', icon: 'none' })
        let key = `list[${index}].isOnSales`
        this.setData({ [key]: !item.isOnSales })
      }
    } catch (err) {
      console.log(err)
      wx.hideLoading()
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  search ({ detail }) {
    this.setData({ keyword: detail })
    this.getList()
  },
  selectFilter () {
    this.setData({ showFilter: true })
  },
  closeFilterPop () {
    this.setData({ showFilter: false })
  },
  changeFilter ({ detail }) {
    this.setData({ reviewStatus: detail.value, reviewStatusStr: detail.label, showFilter: false })
    this.getList()
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      let query = {
        currentPage: this.data.currentPage,
        pageSize: this.data.pageSize
      }
      if (this.data.keyword) query.searchContent = this.data.keyword
      if (this.data.reviewStatus) query.reviewStatus = this.data.reviewStatus
      const { ResultSet } = await getSupplierMaterialList({ ...query })
      this.alloaded = !ResultSet.userData.data.length
      this.data.isEmpty = !ResultSet.userData.data.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.data.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.list.push(...ResultSet.userData.data)
        this.setData({
          list: this.data.list,
          currentPage: this.data.currentPage
        })
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getList () {
    try {
      let query = {
        currentPage: 1,
        pageSize: this.data.pageSize
      }
      if (this.data.keyword) query.searchContent = this.data.keyword
      if (this.data.reviewStatus) {
        if (this.data.reviewStatus === 1) {
          query.isOnSales = true
        } else {
          query.isOnSales = false
        }
        // query.reviewStatus = this.data.reviewStatus
      }
      const { ResultSet } = await getSupplierMaterialList({ ...query })
      this.data.isEmpty = ResultSet.userData.data.length && ResultSet.userData.data.length < this.data.pageSize
        if (ResultSet.userData.data.length) this.data.currentPage = 2
        this.setData({
          list: ResultSet.userData.data,
          currentPage: this.data.currentPage,
          isEmpty: this.data.isEmpty
        })
    } catch (err) {
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  onLoad () {
    this.getList()
  }
})